package com.example.administrator.szb.fragments.fragment_forTab.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SizeUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by stone on 2018/1/20.
 */

public class SupeiAdapter extends RecyclerView.Adapter<SupeiAdapter.MyViewHolder> {
    private Context context;
    private List<Supeis> list;
    public SupeiAdapter( Context context,List list) {
        this.list = list;
        this.context = context;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_supei_card, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        init(holder,position);
    }
    public void changeColor(MyViewHolder holder, final int position){
        holder.reset();
        if (position!=-1){
            holder.select();
        }
    }
    private void init(MyViewHolder holder, final int position){
        if (position==0){
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.cardView.getLayoutParams();
            layoutParams.setMargins(SizeUtils.dip2px(context,14),layoutParams.topMargin,layoutParams.rightMargin,layoutParams.bottomMargin);
            holder.cardView.setLayoutParams(layoutParams);
        }
        //changeColor(holder,position);
        int n=list.get(position).getFirst().size();
        holder.change(n);
        holder.title.setText(""+list.get(position).getTitle());
        holder.content.setText(list.get(position).getOther_require());
        List<Map<String,String>> l = list.get(position).getFirst();
        for(int j=0;j<l.size();j++)
        {
            Map<String,String> map = l.get(j);
            if (j==0){
                for(Map.Entry<String, String> entry:map.entrySet())
                {
                    holder.tv_value_first.setText(entry.getValue());
                    holder.tv_key_first.setText(entry.getValue());
                    QTLog.e(entry.getKey()+":"+entry.getKey());
                }
            }
            if (j==1){
                for(Map.Entry<String, String> entry:map.entrySet())
                {
                    holder.tv_value_two.setText(entry.getValue());
                    holder.tv_key_two.setText(entry.getKey());
                    QTLog.e(entry.getKey()+":"+entry.getValue());
                }
            }
        }


        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onItemClick(position);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
   public static class MyViewHolder extends RecyclerView.ViewHolder
    {
        CardView cardView ;
        TextView title,content;
         LinearLayout ll_content;
         LinearLayout ll_first;
         TextView tv_value_first;
         TextView tv_key_first;
         LinearLayout ll_two;
         TextView tv_value_two;
         TextView tv_key_two;

        public MyViewHolder(View view)
        {
            super(view);
            cardView = view.findViewById(R.id.cardView);
            title = view.findViewById(R.id.title);
            content = view.findViewById(R.id.content);
            ll_content = (LinearLayout) view.findViewById(R.id.ll_content);
            ll_first = (LinearLayout) view.findViewById(R.id.ll_first);
            tv_value_first = (TextView) view.findViewById(R.id.tv_value_first);
            tv_key_first = (TextView) view.findViewById(R.id.tv_key_first);
            ll_two = (LinearLayout) view.findViewById(R.id.ll_two);
            tv_value_two = (TextView) view.findViewById(R.id.tv_value_two);
            tv_key_two = (TextView) view.findViewById(R.id.tv_key_two);
        }
        public void reset(){
            cardView.setCardBackgroundColor(Color.parseColor("#ffffff"));
            title.setTextColor(Color.parseColor("#333333"));
            content.setTextColor(Color.parseColor("#333333"));

             tv_value_first.setTextColor(Color.parseColor("#FF4040"));
             tv_key_first.setTextColor(Color.parseColor("#999999"));
             tv_value_two.setTextColor(Color.parseColor("#FF4040"));
             tv_key_two.setTextColor(Color.parseColor("#999999"));
        }
        public void change(int size){
            //ll_two.setVisibility(View.GONE);
            content.setVisibility(View.GONE);
            //ll_content.setVisibility(View.GONE);
            //ll_first.setVisibility(View.GONE);

            if (size>0){
                ll_content.setVisibility(View.VISIBLE);
                ll_first.setVisibility(View.VISIBLE);
                if (size>1){
                    ll_two.setVisibility(View.VISIBLE);
                }
            }else {
                content.setVisibility(View.VISIBLE);
            }
        }
        public void select(){
            cardView.setCardBackgroundColor(Color.parseColor("#7FB3FF"));
            title.setTextColor(Color.parseColor("#ffffff"));
            content.setTextColor(Color.parseColor("#ffffff"));


            tv_value_first.setTextColor(Color.parseColor("#ffffff"));
            tv_key_first.setTextColor(Color.parseColor("#ffffff"));

            tv_value_two.setTextColor(Color.parseColor("#ffffff"));
            tv_key_two.setTextColor(Color.parseColor("#ffffff"));


        }
    }
    OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(int pos);
    }
}

